package academic.concordia.bankofquebec.bean.virtualwallet;

import org.springframework.transaction.annotation.Transactional;

import academic.concordia.bankofquebec.dao.bankaccount.BankAccountDAO;
import academic.concordia.bankofquebec.entity.BankAccount;
import academic.concordia.bankofquebec.entity.Transaction;
import academic.concordia.bankofquebec.entity.TransactionStatus;
import academic.concordia.bankofquebec.entity.TransactionType;
import academic.concordia.bankofquebec.entity.VirtualWallet;

@Transactional(readOnly = true)
public class VirtualWalletBean implements IVirtualWalletBean {
	
	private BankAccountDAO bankAccountDAO;

	public BankAccountDAO getBankAccountDAO() {
		return bankAccountDAO;
	}

	public void setBankAccountDAO(BankAccountDAO bankAccountDAO) {
		this.bankAccountDAO = bankAccountDAO;
	}

	@Transactional(readOnly = false)
	@Override
	public Transaction depositToVirtualWallet(VirtualWallet eWallet, BankAccount account,
			double amount) {
		Transaction withdrawal = account.withdraw(amount);
		System.out.println("Virtual Wallet " + withdrawal.getStatus() + " " + withdrawal.getMessage());
		if (!withdrawal.isApproved()){
			return withdrawal;
		}
		Transaction deposit = eWallet.deposit(amount);
		System.out.println("Virtual Wallet " + deposit.getStatus() + " " + deposit.getMessage());
		if (!deposit.isApproved()){
			account.revertWithdrawal(withdrawal);
			return deposit;
		}
		withdrawal.setTransactionType(TransactionType.WITHDRAWAL_TO_EWALLET);
		bankAccountDAO.updateBankAccount(account);
		
		return deposit;
	}

	@Override
	public Transaction depositToBankAccount(VirtualWallet eWallet, BankAccount account,
			double amount) {
		// TODO Auto-generated method stub
		return null;
	}

}
